<template>
	<view class="my">
		<user-info :UserInfo="UserInfo" @update="getopenid"></user-info>
		<list-item  @update="getopenid" ref="itemlist" ></list-item>
	</view>
</template>

<script>
	import UserInfo from "../../components/my/UserInfo.vue"
	import ListItem from "../../components/my/ListItem.vue"
	export default {
		components: {
			UserInfo,
			ListItem
		},
		data() {
			return {
				UserInfo: {}
			}
		},
		computed: {
			token() {
				return this.$store.state.token;
			},
			role() {
				return this.$store.state.UserInfo.role
			}
		},
		onShow() {
			this.getopenid()
			// console.log(this.$store.state.userData);
		},
		methods: {
			// 获取openid
			async getopenid() {
				let self = this
				uni.login({
					async success(res) {
						let ret = await self.$Request("/port/getopenid",{ code: res.code })
						let errcode = ret.data.data.errCode
						if(errcode) {
							self.$store.commit("getopenid", {})
						} else {
							self.$store.commit("getopenid", ret.data.data)
							// console.log(self.$store.state.userData.openid);
							self.getUserToken( self.$store.state.userData.openid)
						}
					}
				})
			},
			// 使用openid获取token
			async getUserToken( openid ) {
				let res = await this.$Request("/port/getUserToken", { openid })
				// console.log(res);
				let code = res.data.code 
				if(code == 200) {
					this.$store.commit("getToken", res.data.data)
					this.getUserInfo()
				}
			},
			// 使用token 获取用户信息
			async getUserInfo() {
				if(this.token) {
					let res = await this.$Request("/admin/getMemberData", "", this.token)
					// console.log(res);
					let code = res.data.code 
					if(code == 200) {
						this.UserInfo = res.data.data 
						this.$store.commit("getUserInfo", this.UserInfo)
						this.$refs.itemlist.getUserInfo()
					}
				}
			},
		},
	}
</script>

<style scoped lang="scss">
	.my {
		width: 100%;
		box-sizing: border-box;
		background-color: #f5f5f5;
	}
</style>
